package com.sofwin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.mapper.SysRoleMapper;
import com.sofwin.mapper.SysRoleResourceMapper;
import com.sofwin.pojo.SysRole;
import com.sofwin.pojo.SysRoleExample;
import com.sofwin.pojo.SysRoleResource;
import com.sofwin.pojo.SysRoleResourceExample;
import com.sofwin.system.RoleService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @packageName: com.sofwin.service.impl
 * @user: andyliu
 * @date: 2021/9/25 11:35
 * @email 115176513@qq.com
 * @description: TODO
 */
@Service(interfaceClass = RoleService.class,timeout = 100000)
public class RoleServiceImpl implements RoleService {
    @Autowired
    private SysRoleMapper mapper;

    @Autowired
    private SysRoleResourceMapper roleResourceMapper;
    @Override
    public List<SysRole> queryRole(SysRole role) {
        SysRoleExample ex = new SysRoleExample();
        SysRoleExample.Criteria criteria = ex.createCriteria();
        if(role!=null){
            if(!StringUtils.isEmpty(role.getRoleName())){
                criteria.andRoleNameLike("%"+role.getRoleName()+"%");
            }
            if(role.getStatus()!=null && role.getStatus()!=-1){
                criteria.andStatusEqualTo(role.getStatus());
            }
        }
        List<SysRole> roles = mapper.selectByExample(ex);
        return roles;
    }

    @Override
    public PageInfo<SysRole> queryRoleByPage(SysRole role, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<SysRole> roles = queryRole(role);
        return new PageInfo<SysRole>(roles);
    }

    @Override
    public SysRole queryById(Integer roleId) {
        return mapper.selectByPrimaryKey(roleId);
    }

    @Override
    public boolean saveOrUpdateRole(SysRole role) {
        if(role.getRoleId()!=null){
            return mapper.updateByPrimaryKeySelective(role)==1?true:false;
        }
        return mapper.insertSelective(role)==1?true:false;
    }

    @Override
    public boolean removeRoles(Integer[] ids) {
        int num=0;
        for(Integer id:ids){
            num+=mapper.deleteByPrimaryKey(id);
        }
        return num==ids.length?true:false;
    }

    @Override
    public boolean saveRolePermission(SysRole role, Integer[] ids) {
        // 删除当前角色的所有权限
        int flag=0;
        removeRolePermission(role.getRoleId());
        // 将ids和role中的roleId插入到数据库中
        if(ids!=null && ids.length>0){
            for(Integer id:ids){
                SysRoleResource roleResource = new SysRoleResource();
                roleResource.setRoleId(role.getRoleId());
                roleResource.setResourceId(id);
               flag+= roleResourceMapper.insertSelective(roleResource);
            }
            return flag==ids.length?true:false;
        }
        return true;
    }

    public int removeRolePermission(Integer roleId){
        SysRoleResourceExample ex = new SysRoleResourceExample();
        SysRoleResourceExample.Criteria criteria = ex.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        int flag = roleResourceMapper.deleteByExample(ex);
        return flag;
    }
}
